//
//  SandTableViewController.m
//  Model
//
//  Created by YDJH on 2017/6/5.
//  Copyright © 2017年 YDJH. All rights reserved.
//

#import "SandTableViewController.h"
#import "BuildingConstructionLogicSystem.h"
#import "SandTableView.h"
#import "FurnitureActionView.h"
#import "HZSandTable.h"
#import "HZCommand.h"
#import "HZFurniture_Header.h"
#import "HZRoomLogic.h"
#import "HZOutsideRoomViewController.h"
#import "OutdoorViewController.h"
#import "PresentAnimationClass.h"

@interface SandTableViewController ()

@property (nonatomic,strong)SandTableView *sandTableView;

@end

@implementation SandTableViewController

- (SandTableView *)sandTableView {
    if (!_sandTableView ) {
        __weak typeof(self) weakSelf = self;
        _sandTableView = [[SandTableView alloc] initWithFrame:CGRectZero furnitureClickBlock:^(HZFurniture *furniture, UIImageView *superView) {
            [weakSelf.actionView reloadCollectionWithFurniture:furniture];
            [weakSelf.actionView show];
        }hiddenfurnitureBlock:^(HZHiddenFurniture *hiddenFurniture) {
            [weakSelf actionWithHiddenFurniture:hiddenFurniture];
        }];
    }
    return _sandTableView;
}


- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    self.automaticallyAdjustsScrollViewInsets = NO;
    [self initView];
    // Do any additional setup after loading the view.
}

- (void)setConstruction:(HZConstruction *)construction {
    _construction = construction;
    [self.sandTableView setImage:[UIImage imageNamed:_construction.sandTable.picture]];
    [self.sandTableView reloadWithSandTable:_construction.sandTable];
    
    
}

- (void)initView {
    
    [self.view addSubview:self.sandTableView];
    
    self.sandTableView.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.width);
    self.sandTableView.center = self.view.center;
    
    [self.view addSubview:self.actionView];
    
    UISwipeGestureRecognizer *swip = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(dissmiss)];
    swip.direction = UISwipeGestureRecognizerDirectionDown;
    [self.view addGestureRecognizer:swip];
}


- (void)actionWithHiddenFurniture:(HZHiddenFurniture *)hiddenFurniture {
    
    if ([hiddenFurniture.type isEqualToString:HiddenFurType_Building]) {
        HZBuilding *building = self.construction.sandTable.outsideBuilding[hiddenFurniture.index];
        HZOutsideRoomViewController *firist = [[HZOutsideRoomViewController alloc] init];
        firist.building = building;
        [PresentAnimationClass transitionWithType:AnimationTypeFade withAnimationDirection:AnimationDirectionLeft duration:0.2 fromVC:self toVC:firist];
        
    }else {
        HZOutDoor *outdoor = self.construction.sandTable.outDoor[hiddenFurniture.index];
        OutdoorViewController *outsideVC = [[OutdoorViewController alloc] init];
        outsideVC.outdoor = outdoor;
        [PresentAnimationClass transitionWithType:AnimationTypeFade withAnimationDirection:AnimationDirectionLeft duration:0.2 fromVC:self toVC:outsideVC];
        
    }
}


- (void)actionWithFurniture:(HZFurniture *)furniture command:(HZCommand *)command {
    [super actionWithFurniture:furniture command:command];
    
    
    if ([command.type rangeOfString:enter_Type].location != NSNotFound) { //切换房间
        
    }

    [self showHiddenFurniture];
    
    [self reloadWithSandTable:_construction.sandTable];
}

- (void)showHiddenFurniture {
    
    for (HZFurniture *furnitrue in _construction.sandTable.outsideFurniture) {
        if ([furnitrue.furnitureState isEqualToString:open_state] || [furnitrue.furnitureState isEqualToString:destory_state]) {
            _construction.sandTable.isHiddenFurniture = YES;
            break;
        }
        else if ([furnitrue.furnitureState isEqualToString:close_state] || [furnitrue.furnitureState isEqualToString:lock_state]) {
            _construction.sandTable.isHiddenFurniture = NO;
        }
    }
}

//- (void)enterSandTableWithFurniture:(HZFurniture *)furniture command:(HZCommand *)command {
////    [self reloadWithSandTable:[HZRoomLogic performSelectorWithFurniture:furniture command:command construction:self.construction]];
//    [HZRoomLogic performSelectorWithFurniture:furniture command:command construction:self.construction];
//}
//
//- (void)closeDoorSandTableWithFurniture:(HZFurniture *)furniture command:(HZCommand *)command {
//    
//    [self reloadWithSandTable:_construction.sandTable];
//}

- (void)reloadWithSandTable:(HZSandTable *)sandTable {
    [self.sandTableView reloadWithSandTable:_construction.sandTable];
}




- (void)dissmiss {
    
    [self dismissViewControllerAnimated:YES completion:nil];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
